package controleur;

import java.awt.Color;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.event.MouseInputAdapter;

import vue.PanneauPrincipal;
import vue.VCellule;

import model.Cellule;
import model.Coordonnee;
import model.Environnement;

public class ToogleCellule extends MouseInputAdapter {

	private Environnement env = null;
	private JPanel grid = null;
	private PanneauPrincipal panPrin = null;
	private static boolean template = false;
	private static HashMap<Coordonnee, Cellule> map = null;

	public ToogleCellule(Environnement env, JPanel grid,
			PanneauPrincipal panneauPrincipal) {
		super();
		this.env = env;
		this.grid = grid;
		this.panPrin = panneauPrincipal;
	}

	public void mouseClicked(MouseEvent event) {
		Object o = event.getSource();
		if (o.getClass() != VCellule.class)
			return;
		VCellule v = (VCellule) o;
		if (template) {
			env.addTemplate(map, v.getCoord());
		} else {
			env.toggleCellule(v.getCoord());
		}
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		super.mouseDragged(e);
		Object o = e.getSource();
		if (o.getClass() != VCellule.class)
			return;
		VCellule v = (VCellule) o;
		int l = e.getX() + v.getX();
		Point p = new Point(l, e.getY() + v.getY());
		if (grid.getComponentAt(p) != null)
			if (grid.getComponentAt(p).getClass() == VCellule.class) {
				VCellule c = (VCellule) grid.getComponentAt(p);
				if (template) {
					env.addTemplate(map, c.getCoord());
				} else {

					if (env.getCellule(c.getCoord()) == null) {
						env.vieCellule(c.getCoord());
					}
				}
			}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		super.mouseEntered(e);
		Object o = e.getSource();
		if (o.getClass() != VCellule.class)
			return;
		VCellule v = (VCellule) o;
		if (template) {
			/*
			v.setBorder(BorderFactory.createLineBorder(Color.black));
			*/
			Set<Coordonnee> coords = map.keySet();
			Iterator<Coordonnee> cos = coords.iterator();
			
			while (cos.hasNext()) {
				Coordonnee c = cos.next().relative(v.getCoord());
				if (c.isValid()) {
					VCellule vc = (VCellule) panPrin.getVCelluleAt(
							c.getHauteur(), c.getLargeur());
					if(vc != null)
						vc.setBorder(BorderFactory.createLineBorder(Color.black));
				}
			}
		} else {
			v.setBorder(BorderFactory.createLineBorder(Color.black));
		}
	}

	@Override
	public void mouseExited(MouseEvent e) {
		super.mouseEntered(e);
		Object o = e.getSource();
		if (o.getClass() != VCellule.class)
			return;
		VCellule v = (VCellule) o;
		v.setBorder(BorderFactory.createLineBorder(Color.lightGray));
		if (template) {
			Set<Coordonnee> coords = map.keySet();
			Iterator<Coordonnee> cos = coords.iterator();
			while (cos.hasNext()) {
				Coordonnee c = cos.next().relative(v.getCoord());
				if (c.isValid()) {
					VCellule vc = (VCellule) panPrin.getVCelluleAt(
							c.getHauteur(), c.getLargeur());
					vc.setBorder(BorderFactory
							.createLineBorder(Color.lightGray));
				}
			}
		} else {

		}
	}

	public static boolean isTemplate() {
		return template;
	}

	public static void setTemplate(boolean template,
			HashMap<Coordonnee, Cellule> map) {
		if (map == null) {
			ToogleCellule.template = false;
			ToogleCellule.map = null;
		} else {
			ToogleCellule.template = template;
			if (template == true)
				ToogleCellule.map = map;
			else
				ToogleCellule.map = null;
		}
	}
}
